%macro merge_indiv;

/* Get the set of people */
	proc printto log = "&logdir/merge_indiv.txt" new;
	run;	
	data prim;
		set rlib.filing_status;
		person_id = prim_tin;
		keep person_id;
	run;
	data sec;
		set rlib.filing_status(where=(sec_tin>0));
		person_id = sec_tin;
		keep person_id;
	run;
	data appended;
		set prim sec;
	run;

	proc sort data = appended nodupkey;
		by person_id;
	run;
	
	
	/************************
	*	Now do the merge	*
	************************/
	data rlib.info_returns;
		merge appended
			rlib.w2
			rlib.w2g
			rlib.f1099div
			rlib.f1099int
			rlib.f1099g
			rlib.f1099r
			rlib.f1099ssa
			rlib.f1099b
			rlib.f1099misc
			rlib.f1065_k1
			rlib.f1120s_k1
			rlib.f1099k
			rlib.f5498
			rlib.f1098e;
		by person_id;
		
		/************************************************
		*	Do some calcs, keep only what is necessary	*
		************************************************/
		
		/* reset missings to zero */
		array nums[*] _numeric_;
		do j = 1 to dim(nums);
			if nums(j) = . then nums(j) = 0;
		end;
		
		
		/* Stuff we need for taxsim */
		qdiv = div_qlfy + f1120s_div + f1065_div;
		intrec = f1099int_int + f1120s_int + f1065_int;
		stcg = f1120s_st + f1065_st + gain1;
		ltcg = f1120s_lt + f1065_lt + gain2 + f1099div_cg;
		otherprop = div_ord-div_qlfy+gain3+f1065_royal + f1120s_royal;
		/* can't compute nonprop quite yet */
		/* gssi already done */
		/* ui already done */
		businc = nec + f1120s_ord + f1065_ord + receipts_f1099k;
		
		/* saver's credit contributions */
		tot_contrib = dc_contrib + contrib_ira;
		
		/* withholding */
		withholding = w2_wh + max(0, med_wh - 0.0145*max(wgs+tips,med_wgs))
			+ w2g_wh + f1099div_wh + f1099int_wh + f1099g_wh + 
			f1099r_wh + f1099ssa_wh + f1099misc_wh + f1099b_wh + f1099k_wh;
		
		
		
		keep person_id wgs qdiv intrec stcg ltcg
			otherprop gambling_incm f1099g_ref f1065_gp
			pensions ui businc gssi
			early_roth t_unkn withholding tips
			f1120s_ord f1065_ord nec div_199a f1120s_royal f1065_royal
			sl_int dist_sav_cred tot_contrib;
		
	run;
	
	proc printto;
	run;
		
		


%mend;